﻿
namespace TheCollectiveNetwork.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Objects;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Xml;
    using TCNDataAccessLayer;
    using TheCollectiveNetwork.Classes.GlobalFunctions;
    using TheCollectiveNetwork.Classes.Security;


    // This service allows a user to manage their own credentials, change password etc.    

    [CLSCompliant(false)] // base type exposes _AppDomain which is not cls-compliant as it begins with an underscore so turn cls compliance off for this class
    [EnableClientAccess()]
    [RequiresAuthentication()]
    public class UserCredentialService : LinqToEntitiesDomainService<TheCollectiveNetworkEntities>
    {
        // Changes the password for the current user        
        [Invoke]
        public String ChangePassword(string p_oldPassword, string p_newPassword)
        {
            DTOreturnmessage dtomessage;
            DomainException domainException;
            String ReturnMsg;

            // Initialise variables
            ReturnMsg = String.Empty;
            TCNMembershipProvider tcnmember = new TCNMembershipProvider();

            // Attempt password change
            dtomessage = tcnmember.TCNChangePassword(System.Web.HttpContext.Current.User.Identity.Name, p_oldPassword, p_newPassword);

            // Throw an error if the returncode is not zero
            if (dtomessage.returncode > 0)
            {
                domainException = new DomainException(dtomessage.returnmessage, dtomessage.returncode);
                throw domainException;
            }
            else if (dtomessage.returncode == 0)
            {
                ReturnMsg = dtomessage.returnmessage;
            }

            return ReturnMsg;
                        
            
        }
    }
}


